package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workcorrect.upd1;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;

/**
 * 客观题作业详情
 */
@CrossOrigin
@RestController
public class Workcorrect1HomeworkInfoController extends YxyApiBaseController {

    @Autowired
    ElasticQuesService elasticQuesService;

    @Autowired
    HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @Autowired
    HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workcorrect1/homework/info", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String classId, //
            @RequestParam String studentId, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
        String userId = null;
        String schoolId = null;
        try {
            userId = getProfile(request, response).getId();
            schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        // 单选, 多选
        List<String> quesTypes = new ArrayList<String>();
        quesTypes.add("0");
        quesTypes.add("1");
        List<HomeworkTaskQues> homeworkTaskQuesList = homeworkTaskQuesRepository
                .findByTaskIdAndQuesTypeInOrderByOrderAsc(taskId, quesTypes);
        // 获取试题Id
        List<String> quesIds = new ArrayList<>();
        homeworkTaskQuesList.forEach(homeworkTaskQues -> {
            quesIds.add(homeworkTaskQues.getQuesId());
        });
        List<ElasticYxyYxyQuesResource> questions = elasticQuesService.findByQuesIdIn(quesIds);
        Map<String, ElasticYxyYxyQuesResource> questionMap = new HashMap<String, ElasticYxyYxyQuesResource>();
        questions.forEach(question -> {
            questionMap.put(question.getId(), question);
        });
        // 学生答题
        List<HomeworkTaskReportQues> homeworkTaskReportQuesList = homeworkTaskReportQuesRepository
                .findByClassIdAndStudentIdAndTaskIdAndQuestionTypeIn(classId, studentId, taskId, quesTypes);
        Map<String, String> stuAnswers = new HashMap<String, String>();
        Map<String, String> answers = new HashMap<String, String>();
        homeworkTaskReportQuesList.forEach(homeworkTaskReportQues -> {
            stuAnswers.put(homeworkTaskReportQues.getQuesId(), homeworkTaskReportQues.getAnswerOfMy());
            answers.put(homeworkTaskReportQues.getQuesId(), homeworkTaskReportQues.getAnswerOfQues());
        });
        // return
        List<RespModel> models = new ArrayList<>();
        homeworkTaskQuesList.forEach(homeworkTaskQues -> {
            RespModel model = new RespModel();
            model.index = homeworkTaskQues.getOrder();
            model.ques = null;
            try {
                model.ques = questionMap.get(homeworkTaskQues.getQuesId());
            } catch (Exception e) {
            }
            model.stuAnswer = stuAnswers.get(homeworkTaskQues.getQuesId());
            model.answer = answers.get(homeworkTaskQues.getQuesId());
            models.add(model);
        });
        return models;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        // 试题在任务中的序号
        public Integer index;
        // 试题 Object
        public Object ques;
        // 学生答案
        public String stuAnswer;
        // 正确答案
        public String answer;
        // show
        public boolean show = false;
    }
}
